package com.colorful.insuranceproject.mine;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.base.TitleActivity;
import com.colorful.insuranceproject.bean.PersonInfo;
import com.colorful.insuranceproject.bean.PersonInfoBean;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.request.MineRequest;
import com.lovely3x.common.managements.user.UserManager;
import com.lovely3x.common.utils.Response;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/8/21.
 * <p>
 * 用户信息
 */
public class UserInfoActivity extends TitleActivity {

    private static final int GET_USER_INFO_URL = 0X1;

    public static final String EXTRA_USER_INFO = "extra_user_info";

    public static final String EXTRA_TYPE = "extra_type";

    MineRequest mineRequest;

    /**
     * 真实姓名
     */
    @Bind(R.id.tv_real_name)
    TextView tvRealName;

    /**
     * 身份证号
     */
    @Bind(R.id.tv_id_card_num)
    TextView tvIdCardNum;

    /**
     * 手机绑定
     */
    @Bind(R.id.tv_bind_phone)
    TextView tvBindPhone;

    /**
     * 个人账户
     */
    @Bind(R.id.tv_individual_account)
    TextView tvIndividualAccount;

    /**
     * 认证按钮
     */
    @Bind(R.id.tv_btn)
    TextView tv_btn;

    PersonInfoBean pib;

    private int type;

    @Override
    protected int getContentView() {
        return R.layout.activity_user_info;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setTitle(getString(R.string.user_info));
    }

    @Override
    protected void onViewInitialized() {
        mineRequest = new MineRequest(getHandler());
        if (pib != null) {
            if (type == 1) {
                tv_btn.setVisibility(View.GONE);
            }
            PersonInfo pi = pib.getPersonInfo();
            tvRealName.setText(pi.getResultName());//真实姓名
            tvIdCardNum.setText(pi.getIdCard());//身份证号
            tvBindPhone.setText(pi.getResultPhone());//手机绑定
            tvIndividualAccount.setText(pi.getResultStatus() == 1 ? getString(R.string.enable) : getString(R.string.enablefalse));//个人账户
            tv_btn.setText(pib.getIsAuth() == 1 ? getString(R.string.isauth) : getString(R.string.Real_name_authentication));//个人认证
            tv_btn.setBackgroundResource(pib.getIsAuth() == 1 ? R.drawable.btn_gray_such_as_login : R.drawable.btn_blue_such_as_login);
            if (pib.getIsAuth() == 1)
                tv_btn.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onInitExtras(@NonNull Bundle bundle) {
        super.onInitExtras(bundle);
        pib = bundle.getParcelable(EXTRA_USER_INFO);
        type = bundle.getInt(EXTRA_TYPE);
    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            case GET_USER_INFO_URL:
                if (response.isSuccessful) {
                    showToast(getString(R.string.Authentication_success));
                    finish();
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;
        }
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    /**
     * 实名认证的按钮
     */
    @OnClick(R.id.tv_btn)
    void onClicked() {
        if (UserManager.getInstance().isSigned()) {
            mineRequest.realNameAuthentication(tvRealName.getText().toString(), tvIdCardNum.getText().toString(), GET_USER_INFO_URL);
        } else {
            launchLoginActivity();
        }
    }
}
